const {
  Controller
} = require('egg');
class BaseController extends Controller {
  success(data) {
    if (!data.affectedRows) {
      // 为空说明没有修改数据库，再判断是否为字符串
      if (typeof (data) === 'string') {
        // 为字符串说明为报错信息
        this.ctx.body = {
          success: false,
          message: data,
        };
      } else {
        // 不为字符串则为数据，直接返回data
        this.ctx.body = {
          success: true,
          data,
        };
      }
    } else {
      // 判断修改的条数是否大于等于1
      if (data.affectedRows >= 1) {
        this.ctx.body = {
          success: true,
          message: "操作成功",
        };
      } else {
        this.ctx.body = {
          success: false,
          message: "操作失败"
        };
      }
    }
  }
  error(message) {
    this.ctx.body = {
      success: false,
      message: message
    }
  }
  async house(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.house[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async user(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.user[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async districts(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.districts[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async broker(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.broker[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async group(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.group[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async order(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.order[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async message(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.message[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
  async audit(str) {
    const {
      ctx,
      app
    } = this;
    try {
      const res = await ctx.service.audit[str]();
      this.success(res)
    } catch (error) {
      this.error(error.message)
    }
  }
}
module.exports = BaseController;